package com.intel.two.controller.building;

import com.intel.two.common.LayuiPageVo;
import com.intel.two.common.R;
import com.intel.two.pojo.building.Equipment;
import com.intel.two.service.building.EquipmentService;
import com.intel.two.utils.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/equipment")
public class EquipmentController {
    @Autowired
    private EquipmentService equipmentService;

    @RequestMapping("/equipmentList")
    public LayuiPageVo<Equipment> equipmentPageData(@RequestParam Map<String,Object> params){
        System.out.println("接收的参数"+params);
        return equipmentService.equipmentPageData(params);
    }

    @RequestMapping("/uploadPic")
    public R uploadPic(MultipartFile file) throws Exception {
        System.out.println("上传照片");
        //获得文件名字
        String originalFilename = file.getOriginalFilename();
        //执行上传，输入流，返回服务器文件地址

        String fileUrl = OssUtil.updateLoad(file.getInputStream(), originalFilename);
        System.out.println(fileUrl);

        //将地址返回前端
        return R.ok().data("url",fileUrl);
    }

    @RequestMapping("addEquipment")
    public R addEquipment(@RequestBody Equipment equipment){

        System.out.println("添加用户"+equipment);
        int result = equipmentService.addEquipment(equipment);
        if (result == 1) {
            return R.ok();
        }else{
            return R.error();
        }

    }

    @RequestMapping("delete")
    public R deleteEquipment (Integer id){
        System.out.println("要删除的器材id"+id);
        int result = equipmentService.deleteEquipment(id);
        if (result == 1) {
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping("/deleteEquipments")
    public R deleteEquipments(@RequestParam Map<String, Object> map) {
        System.out.println(map);
        int result = equipmentService.deleteEquipments(map);
        return result >0 ? R.ok() : R.error();
    }

    @RequestMapping("updateEquipment")
    public R updateEquipment(Equipment equipment){
        System.out.println("更新的用户"+equipment);
        int result = equipmentService.updateEquipment(equipment);
        if (result == 1) {
            return R.ok();
        }else{
            return R.error();
        }
    }
}
